function _defineProperty(t, e, a) {
  return e in t ? Object.defineProperty(t, e, {
      value: a,
      enumerable: !0,
      configurable: !0,
      writable: !0
  }) : t[e] = a, t;
}

var app = getApp();
var util = require("../../utils/util.js");
var infoList;

Page(_defineProperty({
  data: {
    infoList:0,
    totalPrice:0,
    selectAllStatus:true,
    cart_list:0,
    userinfo:{}
},
    decre:function(e){
        let store_index = e.currentTarget.dataset.store_index; 
        let goods_index = e.currentTarget.dataset.goods_index; 
        let list = this.data.infoList;
        if(list[store_index].goods[goods_index].cart_info.num <= 1)return;
        list[store_index].goods[goods_index].cart_info.num -= 1;
        app.util.request({
          url:  "entry/wxapp/ReduceCartById",
          data:  {
            cart_id: list[store_index].goods[goods_index].cart_info.id
          },
          success:  function (t)  {
          },

        });
        this.setData({
            infoList:list
        });
        this.totalPrice();
    },
    incre: function(e){
        let store_index = e.currentTarget.dataset.store_index; 
        let goods_index = e.currentTarget.dataset.goods_index; 
        //this.data.infoList[changeCount].count -= 1;
        let list = this.data.infoList;
        list[store_index].goods[goods_index].cart_info.num = parseInt(list[store_index].goods[goods_index].cart_info.num)  + 1;
      app.util.request({
        url: "entry/wxapp/AddCartById",
        data: {
          cart_id: list[store_index].goods[goods_index].cart_info.id
        },
        success: function (t) {
        },

      });
        this.setData({
            infoList:list
        });
        this.totalPrice();
    },
    delete_goods:function(e){
      let store_index = e.currentTarget.dataset.store_index; 
      let goods_index = e.currentTarget.dataset.goods_index; 
      let list = this.data.infoList;
      let delGood=list[store_index].goods.splice(goods_index,1)
      console.log(delGood);
      let del_id = delGood[0].cart_info.id
      this.setData({
        infoList:list
      })
      this.totalPrice()
      app.util.request({
         url: "entry/wxapp/ShopcarDelet",
                data: {
                    cart_id:del_id
                },
                success: function(res) {
                  console.log(res,'已经删除');
                }
      })
    },
    totalPrice:function(){
        console.log('计算了');
        let list = this.data.infoList;                  // 获取购物车列表
        let total = 0;
        for(let i = 0; i<list.length; i++) {         // 循环列表得到每个数据
            for(let j=0;j<list[i].goods.length;j++){
                if(list[i].goods[j].cart_info.selected) {    
                    total += list[i].goods[j].cart_info.num * list[i].goods[j].cart_info.money;     // 所有价格加起来
                }
            }
            
        }
        this.setData({                                // 最后赋值到data中渲染到页面
            infoList: list,
            totalPrice: total.toFixed(2)
        });
    },
    removeThis:function(index){
        this.infoList.splice(index,1)
    },
    selectAll(e) {
        let selectAllStatus = this.data.selectAllStatus;    // 是否全选状态
        selectAllStatus = !selectAllStatus;
        let list = this.data.infoList;
        for(let i = 0; i<list.length; i++) {         // 循环列表得到每个数据
            for(let j=0;j<list[i].goods.length;j++){
                list[i].goods[j].cart_info.selected = selectAllStatus
            }
            
        }
        var user_id = this.data.userinfo.id;
        app.util.request({
          url: "entry/wxapp/ChangeCartSelectedAll",
          data: {
            user_id: user_id
          },
          success: function (t) {
          },

        });
        this.setData({
            selectAllStatus: selectAllStatus,
            infoList: list
        });
        this.totalPrice();                               // 重新获取总价
    },
    selectList(e) {
        let store_index = e.currentTarget.dataset.store_index; 
        let goods_index = e.currentTarget.dataset.goods_index; 
        let list = this.data.infoList;                    // 获取购物车列表   
        list[store_index].goods[goods_index].cart_info.selected = !list[store_index].goods[goods_index].cart_info.selected;     // 改变状态
        this.setData({
            infoList: list
        });
        app.util.request({
          url: "entry/wxapp/ChangeCartSelected",
          data: {
            cart_id: list[store_index].goods[goods_index].cart_info.id
          },
          success: function (t) {
          },

        });
        
        this.totalPrice();                           // 重新获取总价
    },
  onLoad: function(t) {
      console.log(t);
      let e = t;
      let c = this;
      var unkown = decodeURIComponent(e.scene);
      app.setNavigationBarColor(this);
      this.totalPrice();
      var a = this;
      var user_id = '';
      console.log(getApp().xtxx1), getApp().xtxx1 && app.pageOnLoad(a);
      app.getUserInfo(function(t) {
        // null == e.qgjl && app.util.request({
        //     url: "entry/wxapp/DelCar",
        //     cachetime: "0",
        //     data: {
        //         user_id: unkown.id,
        //         store_id: c.data.store_id
        //     },
        //     success: function(t) {
        //         console.log(t.data);
        //     }
        // });
        
        var a = util.formatTime(new Date()).substring(0, 10).replace(/\//g, "-");
            console.log(t, a), c.setData({
                userinfo: t
            }),user_id=t.id
            
      }), app.util.request({
                        url: "entry/wxapp/FindShopInfo",
                        data: {
                            user_id:user_id
                        },
                        success: function(t) {
                                if(t.data != null){
                                    c.setData({
                                        infoList:t.data
                                    });
                                    console.log(infoList);
                                    console.log("成功了");
                                }else{
                                    console.log("setdata里的值为空");
                                }
                                a.totalPrice();
                        },
        
                    });
    },
  submit: function() {
      // var store_ids = this.data.infoList.map(function(v,i){
      //   return v.store.id;
      // });

      var store_ids = this.data.infoList.filter(function(v,i){
        return v.goods.some(function(vv){
          return vv.cart_info.selected
        });
      }).map(function(v,i){
        return v.store.id;
      });

      store_ids = store_ids.join(',');
        var t = this.data.userinfo;
        console.log(t), "" == t.img || "" == t.name ? wx.navigateTo({
            url: "../smdc/getdl"
        }) : wx.navigateTo({
            url: "/zh_cjdianc/pages/takeout/takeoutform?store_ids="+store_ids+"&type=1"+"&zongmoney="+this.data.totalPrice 
        });
    }
}, "hide", function(t) {
  this.setData({
      hide: 1
  });
}));